using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Labirinto_Magico
{
    public class PauseMenu : Microsoft.Xna.Framework.DrawableGameComponent
    {
        List<GUIButton> options = null;
        RenderingData mRenderWrapper = null;
        SoundHandler mSound = null;

        public PauseMenu(Game game)
            : base(game)
        {
            options = new List<GUIButton>();
            mRenderWrapper = (RenderingData)Game.Services.GetService(typeof(RenderingData));
            mSound = (SoundHandler)Game.Services.GetService(typeof(SoundHandler));
        }

        public override void Initialize()
        {
            int halfWidth = Game.GraphicsDevice.Viewport.Width / 2;
            int heightOnFour = Game.GraphicsDevice.Viewport.Height / 4;
            int widthOnFour = halfWidth / 2;

            Vector2 buttonRender = new Vector2(widthOnFour, heightOnFour);

            options.Add(new GUIButton("RESUME", new Vector2(halfWidth, heightOnFour), buttonRender));
            options.Add(new GUIButton("SAVE", new Vector2(halfWidth, heightOnFour * 2), buttonRender));
            options.Add(new GUIButton("MAIN", new Vector2(halfWidth, heightOnFour * 3), buttonRender));

            base.Initialize();   
        }

        public override void Update(GameTime gameTime)
        {
            if (InputHandlerModule.inputHandler.mInput.leftMousePressed)
            {
                if (options.ElementAt(2).PressButton())
                {
                    mSound.ButtonSound();
                    Game.Components.Remove(this);
                    Game.Components.Add(new MainMenu(Game));
                }
                else if (options.ElementAt(1).PressButton())
                {
                    mSound.ButtonSound();
                    SaveGame();
                    Game.Components.Remove(this);
                    Game.Components.Add(new GameLogic(Game, LOAD_FROM.Temporary, 0));
                }
                else if (options.ElementAt(0).PressButton())
                {
                    mSound.ButtonSound();
                    Game.Components.Remove(this);
                    Game.Components.Add(new GameLogic(Game, LOAD_FROM.Temporary, 0));
                }
            }            
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            foreach (GUIButton option in options)
            {
                Texture2D tex = new Texture2D(GraphicsDevice, 32, 32);
                switch (option.mType)
                {
                    case "RESUME":
                        if (option.IsMouseOver())
                            tex = mRenderWrapper.mTextureData.resumeSelected;
                        else
                            tex = mRenderWrapper.mTextureData.resume;
                        break;

                    case "SAVE":
                        if (option.IsMouseOver())
                            tex = mRenderWrapper.mTextureData.saveGameSelected;
                        else
                            tex = mRenderWrapper.mTextureData.saveGame;
                        break;

                    case "MAIN":
                        if (option.IsMouseOver())
                            tex = mRenderWrapper.mTextureData.mainMenuSelected;
                        else
                            tex = mRenderWrapper.mTextureData.mainMenu;
                        break;
                }
                mRenderWrapper.DrawTexture(tex, option.mDimension, option.mPosition, 0, Color.White);
            }

            base.Draw(gameTime);
        }

        private void SaveGame()
        {
            if (File.Exists("tempgame.sav"))
            {
                using (var stream = File.Open("tempgame.sav", FileMode.Open))
                {
                    var binary_formatter = new BinaryFormatter();
                    LabyrinthGameState.gameState = (LabyrinthGameState.State)binary_formatter.Deserialize(stream);
                    SI.LOGIC_GAME_STATE stateMachine = (SI.LOGIC_GAME_STATE)binary_formatter.Deserialize(stream);

                    var stream1 = File.Open("savegame.sav", FileMode.Create);
                    binary_formatter.Serialize(stream1, LabyrinthGameState.gameState);
                    binary_formatter.Serialize(stream1, stateMachine);

                    stream.Close();
                    stream1.Close();
                }
            }
        }
    }
}
